package com.huynv.ochuvietnam;

import android.app.Activity;
import android.content.Context;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.HashMap;
public class textAdapter  extends BaseAdapter{
	private Context mContext;
	private HashMap<Integer, TextView>	views = new HashMap<Integer, TextView>();
	public static final int 			AREA_BLOCK = -1;
	public static final int 			AREA_WRITABLE = 0;
	private int 	displayHeight;
	 // references to our images
	    private TextView[] mThumbIds=null; 
	    private int 						width;
		private int 						height;
	    private item[] itemOchu;
	    private String keywordNow;
	    private String questionNow;
	    private int typeNow;
	    private int lengthOchu;
	    private boolean hasModify=false;
		private String[][]					area;			
		private String[][] 					correctionArea; 
	    public textAdapter(Activity act,Context c,TextView[] thumb,int width, int height,item[] itemOchu,int lengthOchu) {
	        mContext = c;
	        mThumbIds=new TextView[thumb.length];
	        mThumbIds=thumb;
	        Display display = act.getWindowManager().getDefaultDisplay();
	        this.width = width;
			this.height = height;
			this.itemOchu=itemOchu;
			this.lengthOchu=lengthOchu;
			//for(int i=0;i<lengthOchu;i++) itemOchu[i].keyword=resetKeyword(itemOchu[i].keyword);
			//Từ danh sách các từ khóa điền vào mảng ô chữ
			 // Fill area and areaCorrection
	        this.area = new String[this.height][this.width];
	        this.correctionArea = new String[this.height][this.width];
			for(int i=0;i<lengthOchu;i++){
				if (itemOchu[i].type==0){
					int x = itemOchu[i].fromX;
			    	int y = itemOchu[i].fromY;
			    	String text = itemOchu[i].keyword;
			    	for (int t = 0 ; t < itemOchu[i].keyword.length(); t++) {
				    	if (x >= 0 && x < this.height && y+t >= 0 && y+t < this.width)
		    			{
		    				this.area[x][y+t] = " ";//text != null ? String.valueOf(text.charAt(t)) : " ";
		    				this.correctionArea[x][y+t] = String.valueOf(text.charAt(t));
		    			}
			    	}
				}
			}
			Log.e("eeeeeeeeeeeeeee",String.valueOf(this.height)+"_"+String.valueOf(this.width));
	        this.displayHeight = display.getWidth() / this.width;
//	        String temp="";
//	        for(int i=0;i<this.height;i++){
//				for(int j=0;j<this.width;j++){
//					String t=getTextOnXy(i,j,0);
//					temp+="("+t+")";
//				}
//				temp+="\r\n";
//			}
//	        Log.e("11111111111111111111111111111", temp);
	        hasModify=false;
	    }
	    public String resetKeyword(String value){
	    	for(int j=0;j<value.length();j++)
	    	{
	    		value.replace(value.charAt(j),' ');
	    	}
	    	return value;
	    }
	    public int getCount() {
	        return mThumbIds.length;
	    }

	    public Object getItem(int position) {
	        return null;
	    }

	    public long getItemId(int position) {
	        return 0;
	    }
	    public String getQuestionNow(int x,int y){
	    	int x1;
	    	int y1;
	    	int x2;
	    	int y2;
	        String keyword = "";
	        //if (type == 1) alert(x + "." + y);
	        //Log.e("eeeee", String.valueOf(itemOchu.length));
	        //Log.e("eeeee", String.valueOf(itemOchu[0].keyword));
	        for (int k = 0; k < lengthOchu; k++) {   
	        	if (itemOchu[k]==null) break;
	            x1 = itemOchu[k].fromX;
	            y1 = itemOchu[k].fromY;
	            x2 = itemOchu[k].toX;
	            y2 = itemOchu[k].toY;
	            keyword = itemOchu[k].keyword; 
	            if (belong(x, y, x1, y1, x2, y2)) {
	                keywordNow = keyword;
	                questionNow = itemOchu[k].question;                
	                typeNow = itemOchu[k].type;
	                if (belongNgang(x,y,x1,y1,x2,y2)) return questionNow;
	            }//belong
	        }//for
	    	return "";
	    }
	    // create a new ImageView for each item referenced by the Adapter
	    public View getView(int position, View convertView, ViewGroup parent) {
	    	TextView tView=this.views.get(position);;
//	        if (convertView == null) {  // if it's not recycled, initialize some attributes
//	        	tView = new TextView(mContext);
//	        	tView.setLayoutParams(new GridView.LayoutParams(85, 85));
//	            //imageView.setScaleType(TextView.ScaleType.CENTER_CROP);
//	            tView.setPadding(0, 0, 0, 0);
//	        } else {
//	        	tView = (TextView) convertView;
//	        }
	    	//tView = new TextView(mContext);
	    	tView = new TextView(mContext);
	    	tView.setLayoutParams(new GridView.LayoutParams(GridView.LayoutParams.FILL_PARENT, this.displayHeight));
	    	tView.setTextSize((mContext.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == 4 ? 30 : 20);
	    	tView.setGravity(Gravity.CENTER);
	    	//tView.setTextSize((context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == 4 ? 30 : 20);
	    	//tView.setGravity(Gravity.CENTER);
			
	        //imageView.setImageBitmap(mThumbIds[position]);
	        //.setImageResource(mThumbIds[position]);
	    	Log.e("bbbbbbbbbbb", String.valueOf(this.width)+"_"+String.valueOf(this.height));
	    	int x=position/this.width;//hang
	    	int y=position%this.width;//cot
	    	int colorBG=0x00000000;
	    	int colorTDBD=0;
	    	Log.e("cccccccccccccccccc", String.valueOf(this.width)+"_"+String.valueOf(this.height));
	    	String text = this.area[x][y];//getTextOnXy(x, y,0).toUpperCase();
	    	//Log.e("bbbbbbbbbbb", text);
	    	//Log.e("aaaaaaaaaaaaaaaa", String.valueOf(position)+":"+String.valueOf(x)+"_"+String.valueOf(y)+":"+text);
	    	if (text!=null) {
                if (typeNow == 0) {
                    colorBG = 0xf0f8ff00;//0xffff000;
                }
                else {
                    colorBG = 0xf0f8ff00;//0xff00000;
                   
                }
                colorTDBD = 0x000000;
                //text = "";
            } else {
                text = "";
                colorBG = 0x00000000;//0xf0f8ff00;//
                //colorTDBD = "#ffffff";
            }
	    	
	    	tView.setTextColor(0xffffffff);
//	    	if (!hasModify) 
//	    		tView.setText("");
//	    	else
	    	tView.setText(String.valueOf(text));
	        tView.setBackgroundColor(colorBG);
	       
	        return tView;
	    }
	    public String getTextOnXy(int x,int y,int type){
	    	int x1;
	    	int y1;
	    	int x2;
	    	int y2;
	        String keyword = "";
	        //if (type == 1) alert(x + "." + y);
	        //Log.e("eeeee", String.valueOf(itemOchu.length));
	        //Log.e("eeeee", String.valueOf(itemOchu[0].keyword));
	        for (int k = 0; k < lengthOchu; k++) {   
	        	if (itemOchu[k]==null) break;
	            x1 = itemOchu[k].fromX;
	            y1 = itemOchu[k].fromY;
	            x2 = itemOchu[k].toX;
	            y2 = itemOchu[k].toY;
	            keyword = itemOchu[k].keyword; 
	            if (belong(x, y, x1, y1, x2, y2)) {
	                keywordNow = keyword;
	                questionNow = itemOchu[k].question;                
	                typeNow = itemOchu[k].type;                
	                if (type == 0) {//tra ve gia tri
	                    if (belongNgang(x,y,x1,y1,x2,y2)) return String.valueOf(keyword.charAt(y - y1));//ngang
	                    if (belongDoc(x,y,x1,y1,x2,y2)) return String.valueOf(keyword.charAt(x - x1));//doc
	                } else {                    
	                    
	                }//type
	            }//belong
	        }//for
	        return "";
	    	
	    }
	    public void setTextOnXy(int x,int y,int type,String value){
	    	this.area[x][y]=value;
//	    	int x1;
//	    	int y1;
//	    	int x2;
//	    	int y2;
//	        String keyword = "";
//	        //if (type == 1) alert(x + "." + y);
//	        //Log.e("eeeee", String.valueOf(itemOchu.length));
//	        //Log.e("eeeee", String.valueOf(itemOchu[0].keyword));
//	        for (int k = 0; k < lengthOchu; k++) {   
//	        	if (itemOchu[k]==null) break;
//	            x1 = itemOchu[k].fromX;
//	            y1 = itemOchu[k].fromY;
//	            x2 = itemOchu[k].toX;
//	            y2 = itemOchu[k].toY;
//	            keyword = itemOchu[k].keyword; 
//	            if (belong(x, y, x1, y1, x2, y2)) {
//	                keywordNow = keyword;
//	                questionNow = itemOchu[k].question;                
//	                typeNow = itemOchu[k].type;                
//	                if (type == 0) {//tra ve gia tri
////	                    if (belongNgang(x,y,x1,y1,x2,y2)) return String.valueOf(keyword.charAt(y - y1));//ngang
////	                    if (belongDoc(x,y,x1,y1,x2,y2)) return String.valueOf(keyword.charAt(x - x1));//doc
//	                	
//	                	if (belongNgang(x,y,x1,y1,x2,y2)) {
//	                		hasModify=true;
//	                		changeValue(y,k,value);
//	                	}
//	                } else {                    
//	                    
//	                }//type
//	            }//belong
//	        }//for
//	        //return "";
	    	
	    }
	    public boolean belong(int x,int y,int x1,int y1,int x2,int y2) {
	        //Neu la chieu ngang
	        if (x == x1 && x == x2 && y >= y1 && y <= y2) return true;
	        //Chieu doc
	        if (y == y1 && y == y2 && x >= x1 && x <= x2) return true;
	        return false;
	    }
	    public boolean belongNgang(int x,int y,int x1,int y1,int x2,int y2){
	        if (x == x1 && x == x2 && y >= y1 && y <= y2) return true;
	        return false;
	    }
	    public boolean belongDoc(int x,int y,int x1,int y1,int x2,int y2){
	        if (y == y1 && y == y2 && x >= x1 && x <= x2) return true;
	        return false;
	    }
	    public boolean isBlock(int x, int y) {
			return (this.area[x][y] == null);
		}

		public void changeValue(int y,int index, String value) {
//			if (this.itemOchu[x*this.height+y] != null)
//				this.itemOchu[x*this.height+y] = value.toUpperCase();//this.isDraft ? value.toLowerCase() : value.toUpperCase();
			String tempword=this.itemOchu[index].keyword;
			String[] temp=new String[tempword.length()];
			for(int k=0;k<tempword.length();k++) temp[k]=tempword.charAt(k)+"";
			//this.itemOchu[x*this.height+y].keyword=this.itemOchu[x*this.height+y].keyword.regionMatches(true,y, value, y,1);
			temp[y]=value;
			String result="";
			for(int k=0;k<temp.length;k++) result+=temp[k];
			this.itemOchu[index].keyword=result;
			Log.e("Thay doi gia tri moi la "+result,"");
		}
}
